{% extends "index/main.html" -%}
{% block css -%}
<style type="text/css">
    .checkbox + label:before {
        width: 3rem;
        height: 3rem;
    }
    .checkbox + label:after {
        top: -0.7rem;
        left: 1rem;
        width: 1rem;
        height: 2rem;
    }
</style>
{% endblock css -%}
{% block content -%}
<table class="table table-border table-responsive table-hover" style="width: 40rem;">
	<tr>
	   <th>菜单</th>
	   <th>授权</th>
	</tr>
    {% for parent in menus -%}
    {% if parent.types == "1" -%}
    {% set parentId = parent.id -%}
        <tr>
            <td>{{parent.name}}</td>
            <td>
                <input type="checkbox" id="autho_{{parentId}}" onclick="clickParent('{{parentId}}')" class="checkbox">
                <label for="autho_{{parentId}}"></label>
            </td>
        </tr>
        {% for son in menus -%}
        {% if parentId == son.parentId -%}
            <tr>
                <td>
                    <span style="margin-left: 4rem;">{{son.name}}</span>
                </td>
                <td>
                    <input type="checkbox" id="autho_{{son.id}}" name="autho_{{parentId}}" value="{{son.id}}" onclick="clickSon('{{parentId}}', '{{son.id}}')" class="checkbox">
                    <label for="autho_{{son.id}}"></label>
                </td>
            </tr>
            {% set subparentId = son.id -%}
            {% for subson in menus -%}
            {% if subson.types == "3" and subparentId == subson.parentId -%}
                <tr>
                    <td>
                        <span style="margin-left: 6rem;">{{subson.name}}</span>
                    </td>
                    <td>
                        <input type="checkbox" id="autho_{{subson.id}}" name="autho_{{subparentId}}" value="{{subson.id}}" onclick="clickSubson('{{parentId}}', '{{subparentId}}', '{{subson.id}}')" class="checkbox">
                        <label for="autho_{{subson.id}}"></label>
                    </td>
                </tr>
            {% endif -%}
            {% endfor -%}
        {% endif -%}
        {% endfor -%}
    {% endif -%}
    {% endfor -%}
</table>

<script type="text/javascript">
	window.onbeforeunload = function(){
		if('{{id}}' == '{{userId}}'){
			if(sessionStorage.getItem('changeAuth') == null){
		        iceui.post('{{ctx}}/ajaxMenu.html', {}, function (data) {
		            if (data.code == '20') {
		            	sessionStorage.setItem('changeAuth', 'true');
		            }
		        });
			}
		}
	}
	
	ice(function(){
		if('{{menuList}}'!=''){
            var menus=JSON.parse('{{menuList}}');
            var length=menus.length;
            for(var i=0;i<length;i++){
                ice('#autho_'+menus[i].menuId).attr('checked', true);
            }
		}
	});
    
    function clickParent(parentId){
        var parentChecked=ice('#autho_'+parentId+':checked').length==1;
        var sonIds=ice('input[name="autho_'+parentId+'"]');
        var subsonIds=null;
        var length=sonIds.length, sonId=0, lengths=0;
        var menuId=parentId;
        for(var i=0;i<length;i++){
            sonId=sonIds[i].value;
            menuId+=','+sonId;
            sonIds[i].checked=parentChecked;
            subsonIds=ice('input[name="autho_'+sonId+'"]');
            lengths=subsonIds.length;
            for(var j=0;j<lengths;j++){
                menuId+=','+subsonIds[j].value;
                subsonIds[j].checked=parentChecked;
            }
        }
        
        iceui.post(
        	'{{ctx}}/users/ajaxAuthorization.html',
            {'checked':parentChecked, 'parentId':'{{id}}', 'menuId':menuId},
            function(data){
            	if(data.code=='20'){
                    iceui.pop('授权成功');
            	}else{
            		iceui.pop({title: '授权失败', icon: 'warning'});
            	}
            }
        );
    }

    function clickSon(parentId,sonId){
        var parentChecked=ice('#autho_'+parentId+':checked').length==1;
        var sonChecked=ice('#autho_'+sonId+':checked').length==1;
        var subsonIds=ice('input[name="autho_'+sonId+'"]');
        var lengths=subsonIds.length;
        var menuId=sonId;
        
        if(sonChecked){
            if(!parentChecked){
                ice('#autho_'+parentId).attr('checked', true);
                menuId+=','+parentId;
            }
        }else{
            var sonIds=ice('input[name="autho_'+parentId+'"]:checked');
            if(sonIds.length==0){
                ice('#autho_'+parentId).removeAttr('checked');
                menuId+=','+parentId;
            }
        }
        for(var j=0;j<lengths;j++){
            menuId+=','+subsonIds[j].value;
            subsonIds[j].checked=sonChecked;
        }

        iceui.post(
            '{{ctx}}/users/ajaxAuthorization.html',
            {'checked':sonChecked, 'parentId':'{{id}}', 'menuId':menuId},
            function(data){
                if(data.code=='20'){
                    iceui.pop('授权成功');
                }else{
                    iceui.pop({title: '授权失败', icon: 'warning'});
                }
            }
        );
    }

    function clickSubson(parentId,subparentId,sonId){
        var parentChecked=ice('#autho_'+parentId+':checked').length==1;
        var subparentChecked=ice('#autho_'+subparentId+':checked').length==1;
        var sonChecked=ice('#autho_'+sonId+':checked').length==1;
        var menuId=sonId;
        
        if(sonChecked){
            if(!parentChecked){
                ice('#autho_'+parentId).attr('checked', true);
                menuId+=','+parentId;
            }
            if(!subparentChecked){
                ice('#autho_'+subparentId).attr('checked', true);
                menuId+=','+subparentId;
            }
        }
        
        iceui.post(
            '{{ctx}}/users/ajaxAuthorization.html',
            {'checked':sonChecked, 'parentId':'{{id}}', 'menuId':menuId},
            function(data){
                if(data.code=='20'){
                    iceui.pop('授权成功');
                }else{
                    iceui.pop({title: '授权失败', icon: 'warning'});
                }
            }
        );
    }
</script>
{% endblock content -%}